package com.kkb.retailer.center.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.kkb.retailer.center.api.StoreSeckillService;
import com.kkb.retailer.center.common.dto.StoreSeckillDto;
import com.kkb.retailer.center.common.pojo.StoreSeckill;
import com.kkb.retailer.center.common.util.OrderUtil;
import com.kkb.retailer.center.server.mappers.StoreSeckillMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class StoreSeckillServiceImpl implements StoreSeckillService {

    @Autowired
    private StoreSeckillMapper storeSeckillMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public Map<String, Object> queryAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<StoreSeckill> storeSeckills = storeSeckillMapper.selectAll();
        PageInfo<StoreSeckill> pageInfo = new PageInfo<>(storeSeckills);
        ArrayList<StoreSeckillDto> storeSeckillDtos = new ArrayList<>();
        for (StoreSeckill storeSeckill : pageInfo.getList()) {
            StoreSeckillDto storeSeckillDto = new StoreSeckillDto();
            BeanUtils.copyProperties(storeSeckill,storeSeckillDto);
            storeSeckillDtos.add(storeSeckillDto);
        }
        for (StoreSeckillDto storeSeckillDTO : storeSeckillDtos){
            String statusStr = OrderUtil.checkActivityStatus(storeSeckillDTO.getStartTime(),
                    storeSeckillDTO.getStopTime(), storeSeckillDTO.getStatus());
            storeSeckillDTO.setStatusStr(statusStr);
        }
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("data",storeSeckillDtos);
        map.put("totalElements", pageInfo.getTotal());
        return map;
    }



    @Override
    public int saveOrUpdate(StoreSeckill storeSeckill) {

        int result = 0;
        Long goodsId = storeSeckill.getGoodsId();
        Example example = new Example(StoreSeckill.class);
        example.createCriteria().andEqualTo("goodsId",goodsId);
        int count = storeSeckillMapper.selectCountByExample(example);
        if(count==0){
            //增加秒杀商品
            result = storeSeckillMapper.insertSelective(storeSeckill);
        }else{
            //修改商品
            result = storeSeckillMapper.updateByPrimaryKeySelective(storeSeckill);
        }

        //以下是更新Redis缓存

        String goodidstr = String.valueOf(storeSeckill.getGoodsId());
        String key = "goods-stock_" + goodidstr;
        System.out.println("key:"+key);
        String value = String.valueOf(storeSeckill.getStock());
        System.out.println("value:"+value);


        redisTemplate.boundValueOps(key).set(value);
        redisTemplate.expire(key,storeSeckill.getEndTimeDate().getTime(), TimeUnit.SECONDS);

        return result;
    }
}
